/**
 * 
 */
package service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import model.Datentraeger;
import model.Datentraeger.DatentraegerInfo;
import model.Film;
import service.DatentraegerService;
import service.FilmService;
import service.util.ServiceAnnotation;
import service.util.ServiceException;
import service.util.ServiceFactory;
import controller.util.JDBCConnectionController;
import dao.DAO;
import dao.DatentraegerSqlDAO;
import dao.SqlDao;
import dao.util.DaoContextEnum;


@SuppressWarnings("serial")
@ServiceAnnotation(serviceName = "DatentraegerService", daos = {"DatentraegerSqlDAO"})
public class DatentraegerServiceImpl implements DatentraegerService
{
	private DAO<Datentraeger> dao;
	
	/* (non-Javadoc)
	 * @see at.spengergasse.service.DatentraegerService#deleteDatentraeger(at.spengergasse.model.Datentraeger)
	 */
	
	public int deleteDatentraeger(Datentraeger dat)
	{
		try
		{
			Connection conn = JDBCConnectionController.getConnection();
			FilmService filmService = (FilmService) ServiceFactory.getServiceForName("FilmService", DaoContextEnum.MYSQL);
			List<Film>datFilme = filmService.getFilme(dat.getId().toString());
			for(Film actFilm:datFilme){
				actFilm.removeDatentraeger();
				filmService.saveToDatabase(actFilm);
			}
			if(((SqlDao<Datentraeger>) dao).delete(conn, dat)==1){
				conn.commit();
				return 1;
			}
			conn.rollback();
			return 0;
		} catch (SQLException e)
		{
			throw new ServiceException(e.getMessage(), e);
		}
	}

	/* (non-Javadoc)
	 * @see at.spengergasse.service.DatentraegerService#getDatentraegerById(java.lang.String)
	 */
	
	public Datentraeger getDatentraegerById(String id)
	{
		try
		{
			Long datId = Long.parseLong(id);
			return (((SqlDao<Datentraeger>) dao).findById(JDBCConnectionController.getConnection(), datId));
		} catch (SQLException e)
		{
			throw new ServiceException(e.getMessage(), e);
		} catch (NumberFormatException e)
		{
			throw new ServiceException(e.getMessage(), e);
		}
	}

	/* (non-Javadoc)
	 * @see at.spengergasse.service.DatentraegerService#getDatentraegerForName(java.lang.String)
	 */
	
	public Datentraeger getDatentraegerForName(String Datentraeger)
	{
		try
		{
			return (((DatentraegerSqlDAO) dao).findByName(JDBCConnectionController.getConnection(), Datentraeger));
		} catch (SQLException e)
		{
			throw new ServiceException(e.getMessage(), e);
		}
	}

	/* (non-Javadoc)
	 * @see at.spengergasse.service.DatentraegerService#getDatentraegeren()
	 */
	@Override
	public List<Datentraeger> getDatentraegers()
	{
		try
		{
			return ((SqlDao<Datentraeger>) dao).findAll(JDBCConnectionController.getConnection());
		} catch (SQLException e)
		{
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	public ArrayList<DatentraegerInfo> getDatentraegerInfos()
	{
		List<Datentraeger>allDat=getDatentraegers();
		ArrayList<DatentraegerInfo>infos=new ArrayList<DatentraegerInfo>();
		Iterator<Datentraeger>ite=allDat.iterator();
		while(ite.hasNext()){
			infos.add(ite.next().getInfo());
		}
		return infos;
	}

	/* (non-Javadoc)
	 * @see at.spengergasse.service.DatentraegerService#saveToDatabase(model.Datentraeger)
	 */
	
	public Datentraeger saveToDatabase(Datentraeger dat)
	{
		try
		{
			Connection conn = JDBCConnectionController.getConnection();
			if(dat == null){
				throw new IllegalArgumentException("Datentraeger must not be null!");
			}
			dat = ((SqlDao<Datentraeger>) dao).save(conn, dat);
			if(!dat.isNew()){
				conn.commit();
				return dat;
			}
			conn.rollback();
			return null;
		} catch (SQLException e)
		{
			throw new ServiceException(e.getMessage(), e);
		}
	}

	
	public DAO<Datentraeger> getDao()
	{
		return dao;
	}

	public void setDatentraegerSqlDAO(DAO<Datentraeger> dao)
	{
		this.dao = dao;
	}

	
}
